package com.sgb.kjwl.view.ui.activity.enterprise;

import android.view.View;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPublishServiceCasesBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.TransactionCasesEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.MemberEvent;
import com.sgb.kjwl.view.ui.adapter.enterprise.DealedCasesPublishAdapter;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceDealedCasesActivityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * Time:2021/4/16
 * Author:黄淋
 * Description:发布服务-成交案例
 */
public class PublishServiceDealedCasesActivity extends AppActivity {
    ActivityPublishServiceCasesBinding mBinding;

    @Inject
    PublishServiceDealedCasesActivityModel mModel;
    private DealedCasesPublishAdapter dealedCasesPublishAdapter;
    private boolean mIsLoadMore;
    private String id;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder()
                .appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_publish_service_cases);

        mBinding.actionBar.findViewById(R.id.right_view).setOnClickListener(this);
        mBinding.tvBack.setOnClickListener(this);
        mBinding.tvOk.setOnClickListener(this);
        initAdapter();
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        mModel.ldCaseList.observe(this, list -> showCaseList(list, mIsLoadMore));
        mModel.ldCaseDelete.observe(this, a -> {
            if (a.equals("true")) {
                mIsLoadMore = false;
                mModel.getTransactionList(this, false, id);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (id != null && !id.equals("")) {
            mIsLoadMore = false;
            mModel.getTransactionList(this, false, id);
        }
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        dealedCasesPublishAdapter = new DealedCasesPublishAdapter(this, null);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setAdapter(dealedCasesPublishAdapter);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIsLoadMore = false;
                mModel.getTransactionList(PublishServiceDealedCasesActivity.this, false, id);
            }

            @Override
            public void onLoadMore() {
                mIsLoadMore = true;
                mModel.getTransactionList(PublishServiceDealedCasesActivity.this, true, id);
            }
        });
        dealedCasesPublishAdapter.OnCallPhone((entity, status) -> {
            if (status == 2) {
                DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mModel.deleteTransaction(PublishServiceDealedCasesActivity.this, entity.getId());
                    }
                });
            } else if (status == 1) {
                ActivityJump.jumpToActivity(PublishServiceDealedCasesActivity.this, PublishAddDealedCasesActivity.class, Pair.create(RouterKey.DATA, entity));
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_view:
                if (!ClickUtils.isDoubleClick()) {
                    ActivityJump.jumpToActivity(PublishServiceDealedCasesActivity.this, PublishAddDealedCasesActivity.class, Pair.create("id", id));
                }
                break;
            case R.id.tv_back:
                if (!ClickUtils.isDoubleClick()) {
                    ActivityJump.jumpToActivity(PublishServiceDealedCasesActivity.this, PublishServiceActivity.class, Pair.create("id", id));
                }
                break;
            case R.id.tv_ok:
                if (!ClickUtils.isDoubleClick()) {
                    PublishServiceNodeActivity.startactivity(this,id);
                }
                break;
        }
    }

    /**
     * 案例数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showCaseList(List<TransactionCasesEntity> list, boolean isLoadMore) {
        mBinding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.loadMoreComplete();
                dealedCasesPublishAdapter.addDatas(list);
            } else {
                mBinding.recyclerView.setNoMore(true);
            }
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.empty.setVisibility(View.GONE);
                dealedCasesPublishAdapter.setDatas(list);
                mBinding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                dealedCasesPublishAdapter.setDatas(null);
                mBinding.recyclerView.setVisibility(View.GONE);
                mBinding.empty.setVisibility(View.VISIBLE);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(MemberEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_SERVICE_FINISHDealedCases) {
            finish();
        }
        EventBus.getDefault().removeStickyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
